/*
    Copyright (C) 2013 maik.jablonski@jease.org

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sample.web.project;

import jfix.db4o.Database;
import jfix.util.Natural;
import jfix.zk.ObjectEditor;
import jfix.zk.Selectfield;
import jfix.zk.Textarea;
import jfix.zk.Textfield;
import sample.domain.Person;
import sample.domain.Project;

public class Editor extends ObjectEditor<Project> {

	Textfield title = new Textfield();
	Textarea description = new Textarea();
	Selectfield manager = new Selectfield();

	public void init() {
		add("Title", title, "Please enter the title of the project.");
		add("Description", description,
				"Please enter a description for the project.");
		add("Manager", manager, "Please select the manager of the project.");
	}

	public void load() {
		title.setText(getObject().getTitle());
		description.setText(getObject().getDescription());
		manager.setSelection(Natural.sort(Database.query(Person.class)),
				getObject().getManager());
	}

	public void save() {
		getObject().setTitle(title.getText());
		getObject().setDescription(description.getText());
		getObject().setManager((Person) manager.getSelectedValue());
		Database.save(getObject());
	}

	public void delete() {
		Database.delete(getObject());
	}

	public void validate() {
		validate(title.isEmpty(), "Title is required");
		validate(manager.isEmpty(), "Manager is required");
	}

}
